<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="1200"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="排序" field="sort_order">
        <a-input v-model="formData.sort_order" placeholder="请输入排序" />
      </a-form-item>
      <a-form-item label="金融分类" field="finance_category">
        <a-select
            v-model="formData.finance_category"
            :options="postData"

            allow-clear
            placeholder="请选择金融分类" />
      </a-form-item>
      <a-form-item label="金融机构" field="finance_institution">
        <a-select
            v-model="formData.finance_institution"
            :options="postDatacate"

            allow-clear
            placeholder="请选择金融机" />
      </a-form-item>
<!--      <a-form-item label="时间" field="activity_time_range">-->
<!--        <a-range-picker v-model="formData.activity_time_range" :show-time="true"  mode="date"  />-->
<!--      </a-form-item>-->
      <a-form-item label="开始时间" field="start_time">
        <a-date-picker v-model="formData.start_time" :show-time="true" mode="date" placeholder="请选择开始时间" />
      </a-form-item>
      <a-form-item label="结束时间" field="end_time">
        <a-date-picker v-model="formData.end_time" :show-time="true" mode="date" placeholder="请选择结束时间" />
      </a-form-item>
      <a-form-item label="展示/下架" field="is_display">
        <sa-switch v-model="formData.is_display" checkedText="是"  uncheckedText="否"/>
      </a-form-item>
      <a-form-item label="金融标题" field="finance_title">
        <a-input v-model="formData.finance_title" placeholder="请输入金融标题" />
      </a-form-item>
      <a-form-item label="产品标签" field="product_tags">
        <a-select
            v-model="formData.product_tags"
            :options="postDataAll"
            multiple
            allow-clear
            placeholder="请选择产品标签" />

      </a-form-item>
      <a-form-item label="产品详情" field="product_details">
        <ma-wangEditor v-model="formData.product_details" :height="400" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/finance'
import commonApi from '@/api/common'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const postData = ref([])
const postDatacate = ref([])
const postDataAll = ref([])

let title = computed(() => {
  return '金融列表' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  finance_id: null,
  lang_code: '',
  sort_order: 0,
  finance_category: '',
  finance_institution: '',
  start_time: '',
  end_time: '',
  is_display: 0,
  finance_title: '',
  product_tags: [],
  product_details: '',
})

// 验证规则
const rules = {
  lang_code: [{ required: true, message: '语言代码必需填写' }],
  finance_category: [{ required: true, message: '金融分类必需填写' }],
  finance_institution: [{ required: true, message: '金融机构必需填写' }],
  finance_title: [{ required: true, message: '金融标题必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {

  const postResp = await api.getcate({classification_type:5})
  console.log(postResp.data)
  postData.value = postResp.data

 const postRespcte = await commonApi.commonGet('/financeInstitution/financeInstitution/getSelect')
 
 postDatacate.value = postRespcte.data

  const postRespctes = await api.getcate({classification_type:10})
  console.log(postRespctes.data)
  postDataAll.value = postRespctes.data

  postDataAll
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
  
  
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.finance_id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.finance_id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
